<?php
namespace Modules\Service\Logic;

use App\Models\Coupon;
use App\Models\Draw;
use App\Models\OilCard;
use App\Models\User;
use App\Models\UserCoupon;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;
use App\Models\Configure;

class OilCardLogic extends BaseLogic{
    //绑定油卡
    public static function bindOilCard($userId,$card_no,$type,$name,$mobile){
        try {
            //查询用户
            $userData = User::find($userId);
            if (!$userData) return fail(1014, "用户不存在");
            //查询用户的油卡
            $cardData = OilCard::where('user_id', $userId)->count();
            if ($cardData < 1) {

                // 如果是第一次绑定油卡，需要赠送相应的优惠券
                /*
                $bindCouponIds = Configure::bindOilCardCoupon();
                if ($bindCouponIds) {
                    foreach ($bindCouponIds as $couponId) {
                        UserCoupon::sendToUser($couponId, $userData->id);
                    }
                }
                */

                //判断用户是否存在上级
                if ($userData->parent_id) {
                    /*
                    $regCouponIds = Configure::parentRegCoupon();
                    if ($regCouponIds) {
                        foreach ($regCouponIds as $couponId) {
                            UserCoupon::sendToUser($couponId, $userData->parent_id);
                        }
                    }
                    */
                    //赠送抽奖机会
                    $drawData = Draw::where("user_id", $userData->parent_id)->first();
                    if ($drawData) {
                        $drawData->num = $drawData->num + 1;
                        $drawData->save();
                    } else {
                        $draw = new Draw();
                        $draw->user_id = $userData->parent_id;
                        $draw->num = 1;
                        $draw->save();
                    }
                }
            }
            //实例化油卡对象
            $oil = new OilCard();
            $oil->user_id = $userData->id;
            $oil->card_no = $card_no;
            $oil->mobile = $mobile;
            $oil->type = $type;
            $oil->name = $name;
            $oil->status = 1;
            $oil->save();
            return success();
        }catch (\Exception $e){
            Log::error($e);
            return fail(500,"系统错误");
        }
    }

    //查询油卡
    public static function userHaveOilCard($userId){
        try {
            //获取用户信息
            $userData = User::find($userId);
            if (!$userData) return fail(1014, "用户不存在");
            //查询油卡
            $oilCardData = OilCard::where("user_id",$userId)->where("status",1)->select("id","card_no","type")->get();
            return success($oilCardData);
        }catch (\Exception $e){
            Log::error($e);
            return fail(500,"系统错误");
        }
    }

    //删除油卡
    public static function userDeleteOilCard($userId,$oilCardId){
        try{
            //查询用户
            $userData = User::find($userId);
            if (!$userData)return fail(1014,"用户不存在");
            //查询油卡信息
            $oilCardData = OilCard::find($oilCardId);
            if (!$oilCardData)return fail(1012,"油卡不存在");

            $oilCardData->status = 0;
            $oilCardData->save();
            return success();
        }catch (\Exception $e){
            Log::error($e);
            return fail(500,"系统错误");
        }
    }
}